package com.un.ebs.handler;

import com.un.ebs.businessObject.service.SQLStrategy;
import com.un.ebs.core.enums.DbTypeEnum;
import com.un.ebs.sys.config.HandlerDbType;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @Author zc
 * @ClassName HandlerDbProcessor
 * @Description
 * @Date 2021/1/5
 */
@Component
public class HandlerDbProcessor implements ApplicationContextAware {

    /**
     * 获取所有的策略Beanclass 加入HandlerOrderContext属性中
     *
     * @param applicationContext
     * @throws BeansException
     */
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        //获取所有策略注解的Bean
        Map<String, Object> dbStrategyMap = applicationContext.getBeansWithAnnotation(HandlerDbType.class);
        dbStrategyMap.forEach((k, v) -> {
            Class<SQLStrategy> dbStrategyClass = (Class<SQLStrategy>) v.getClass();
            DbTypeEnum value = dbStrategyClass.getAnnotation(HandlerDbType.class).value();
            //将class加入map中,type作为key
            HandlerDbContext.dbStrategyBeanMap.put(value.getDesc(), dbStrategyClass);
        });
    }
}
